package com.qs.redis.jedis;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 使用jedis连接redis-cluster集群
 *
 * @author qinyupeng
 * @since 2019-12-13 14:57:34
 */
public class JedisClientDemo {

    public static void main(String[] args) {
        Set<HostAndPort> hostAndPortSet = new HashSet<>();
        hostAndPortSet.add(new HostAndPort("49.235.93.80", 7000));
        hostAndPortSet.add(new HostAndPort("49.235.93.80", 7001));
        hostAndPortSet.add(new HostAndPort("49.235.93.80", 7002));
        hostAndPortSet.add(new HostAndPort("49.235.93.80", 7003));
        hostAndPortSet.add(new HostAndPort("49.235.93.80", 7004));
        hostAndPortSet.add(new HostAndPort("49.235.93.80", 7005));
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //构建jedisCluster对象
        JedisCluster jedisCluster = new JedisCluster(hostAndPortSet, 10000, 10000,
                3, "qin4012@", jedisPoolConfig);

        //通过jedisCluster对象操作redis
        String value = jedisCluster.get("name");
        System.out.println(value);

        List<String> userNames = jedisCluster.lrange("userNames", 0, -1);
        System.out.println(userNames);
    }
}
